package flc.ast.utils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.bean.PhoneAlbumBean;

public class AlbumManager {
    private static AlbumManager instance;
    private final List<PhoneAlbumBean.ClassBean> phoneAlbumBeanList = new ArrayList<>();

    private AlbumManager() {

    }

    public synchronized static AlbumManager getInstance() {
        if (instance == null) {
            instance = new AlbumManager();
        }
        return instance;
    }

    public List<PhoneAlbumBean.ClassBean> getPhoneAlbumBeans() {
        return phoneAlbumBeanList;
    }

    public void addPhoneAlbum(List<PhoneAlbumBean.ClassBean> phoneAlbumBeanList) {
        boolean needNotify = false;
        for (PhoneAlbumBean.ClassBean albumBean : phoneAlbumBeanList) {
            addPhoneAlbum(albumBean, false);
            needNotify = true;
        }
        if (needNotify) {
            for (IDataChangeListener listener : listeners) {
                listener.onDataChanged(hasData());
            }
        }
    }

    public void deletePhoneAlbum(List<PhoneAlbumBean.ClassBean> phoneAlbumBeanList) {
        boolean needNotify = false;
        for (PhoneAlbumBean.ClassBean albumBean : phoneAlbumBeanList) {
            deletePhoneAlbum(albumBean, false);
            needNotify = true;
        }
        if (needNotify) {
            for (IDataChangeListener listener : listeners) {
                listener.onDataChanged(hasData());
            }
        }
    }

    public void addPhoneAlbum(PhoneAlbumBean.ClassBean phoneAlbumBean) {
        addPhoneAlbum(phoneAlbumBean, true);

    }

    private void addPhoneAlbum(PhoneAlbumBean.ClassBean phoneAlbumBean, boolean needNotify) {
        if (phoneAlbumBeanList.contains(phoneAlbumBean)) {
            return;
        }
        phoneAlbumBeanList.add(phoneAlbumBean);
        if (needNotify) {
            for (IDataChangeListener listener : listeners) {
                listener.onDataChanged(hasData());
            }
        }
    }

    public void deletePhoneAlbum(PhoneAlbumBean.ClassBean phoneAlbumBean) {
        deletePhoneAlbum(phoneAlbumBean, true);
    }

    private void deletePhoneAlbum(PhoneAlbumBean.ClassBean phoneAlbumBean, boolean needNotify) {
        phoneAlbumBeanList.remove(phoneAlbumBean);
        if (needNotify) {
            for (IDataChangeListener listener : listeners) {
                listener.onDataChanged(hasData());
            }
        }
    }

    public int hasData() {
        return phoneAlbumBeanList.size();
    }

    public void clearPhoneAlbum() {
        phoneAlbumBeanList.clear();
    }

    private List<IDataChangeListener> listeners;

    public void addListener(IDataChangeListener listener) {
        if (listener == null) return;
        if (listeners == null) {
            listeners = new ArrayList<>();
        }
        if (!listeners.contains(listener)) {
            listeners.add(listener);
        }
    }

    public void removeListener(IDataChangeListener listener) {
        if (listeners == null) return;
        listeners.remove(listener);
    }

    public interface IDataChangeListener {

        void onDataChanged(int currentSize);

    }
}
